/// 设置页面
part of pda.page;

class PageSetting extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _PageSettingState();
  }
}

class _PageSettingState extends State<PageSetting> {
  final TextEditingController serverTxtController = TextEditingController();
  final TextEditingController portTxtController = TextEditingController();
  Setting setting;

  @override
  void initState() {
    super.initState();
    loadSetting();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('设置'),
        ),
        body: ListView(
          children: <Widget>[
            Padding(
              padding: EdgeInsets.all(8),
              child: Row(
                children: <Widget>[
                  Container(
                      padding: EdgeInsets.only(right: 8.0),
                      child: Text('服务器地址:')),
                  Expanded(
                    child: TextField(
                      controller: serverTxtController,
                      decoration: InputDecoration(
                          hintText: '请输IP地址或域名',
                          suffixIcon: IconButton(
                            icon: Icon(Icons.remove_circle),
                            onPressed: () {
                              serverTxtController.clear();
                            },
                          )),
                    ),
                  )
                ],
              ),
            ),
            Padding(
              padding: EdgeInsets.all(8),
              child: Row(
                children: <Widget>[
                  Container(
                      padding: EdgeInsets.only(right: 8.0),
                      child: Text('服务器端口:')),
                  Expanded(
                    child: TextField(
                      controller: portTxtController,
                      decoration: InputDecoration(
                          hintText: '请输入服务器端口',
                          suffixIcon: IconButton(
                            icon: Icon(Icons.remove_circle),
                            onPressed: () {
                              portTxtController.clear();
                            },
                          )),
                    ),
                  )
                ],
              ),
            ),
            Padding(
              padding: EdgeInsets.all(8),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: <Widget>[
                  FlatButton(
                    color: Colors.blue,
                    textColor: Colors.white,
                    child: Text('恢复默认值'),
                    onPressed: reset,
                  ),
                  FlatButton(
                    color: Colors.blue,
                    textColor: Colors.white,
                    child: Text('保存'),
                    onPressed: save,
                  ),
                ],
              ),
            ),
          ],
        ));
  }

  void loadSetting() {
    // showSuccess(context, '加载设置信息成功！');
    setting = Setting.fromPref();
    serverTxtController.text = setting.server;
    portTxtController.text = setting.port.toString();
  }

  void reset() {
    serverTxtController.text = '192.168.1.120';
    portTxtController.text = '8080';
  }

  void save() {
    var server = serverTxtController.text;
    var port = int.tryParse(portTxtController.text, radix: 10);
    if (server.isEmpty) {
      showError(context, '服务器地址不能为空！');
      return;
    }
    if (port == null || port > 65535 || port < 1) {
      showError(context, '服务器端口无效，应为1~65535之间的数值！');
      return;
    }
    setting.server = server;
    setting.port = port;
    setting.save();
    showSuccess(context, '保存成功！');
  }
}
